package com.lhy.wcss.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhy.wcss.common.utls.SessionUtils;
import com.lhy.wcss.model.dto.DeleteDTO;
import com.lhy.wcss.model.dto.PageDTO;
import com.lhy.wcss.model.dto.StatDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lhy.wcss.common.JsonResponse;
import com.lhy.wcss.service.WhitelistSettingService;
import com.lhy.wcss.model.domain.WhitelistSetting;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Controller
@RequestMapping("/whitelistSetting")
public class WhitelistSettingController {

    private final Logger logger = LoggerFactory.getLogger( WhitelistSettingController.class );

    @Autowired
    private WhitelistSettingService whitelistSettingService;
    /*登录请求
     * login.html
     * Service返回值为WhitelistSetting对象
     * 通过JsonResponse返回  统一返回数据格式
     * */
    @PostMapping("/login")
    @ResponseBody
    public JsonResponse login(WhitelistSetting whitelistSetting, HttpSession session){
        WhitelistSetting user=whitelistSettingService.login(whitelistSetting);
        if(Objects.nonNull(user)){
            // session.setAttribute(BaseConstant.LOGIN_USER_KEY,user);// BaseConstant.LOGIN_USER_KEY，通过定义接口常数，避免魔法值
            SessionUtils.saveCurUser(user);
        }
        JsonResponse<WhitelistSetting> success = JsonResponse.success(user);
        return success;
    }
    /*
     * 条件加分页查询
     * 查询白名单所以用户
     * 返回所有用户
     * PageDTO避免前端没有传数据
     * 开启  分页后返回值的结构发生变化，
     * 在前端需要对数据进行重新解析，将其解析成正确的格式
     * whitelistSetting 条件查询，前端根据选择的条件发送到后端进行根据条件查询
     * VO视图层对象
     * */
    @GetMapping("list")
    @ResponseBody
    public JsonResponse list(PageDTO pageDTO, WhitelistSetting whitelistSetting) {
        Page<WhitelistSetting> page = whitelistSettingService.pageList(pageDTO, whitelistSetting);
        return JsonResponse.success(page);
    }

    @GetMapping("stat")
    @ResponseBody
    public JsonResponse stat() {
        List<StatDTO> list = whitelistSettingService.stat();
        return JsonResponse.success(list);
    }

    @PostMapping("update")
    @ResponseBody
    public JsonResponse update(WhitelistSetting whitelistSetting) {
        return JsonResponse.success(whitelistSettingService.updateById(whitelistSetting));
    }

    @GetMapping("removeById")
    @ResponseBody
    public JsonResponse removeById(Long id) {
        boolean b = whitelistSettingService.removeById(id);
        return JsonResponse.success(b);
    }
    @GetMapping("logout")
    @ResponseBody
    public JsonResponse logout() {
        SessionUtils.removeCurUser();
        return JsonResponse.success(true);
    }
    @PostMapping("/removeById")
    @ResponseBody
    public JsonResponse removeById(@RequestBody DeleteDTO deleteDTO){
        boolean b = whitelistSettingService.removeById(deleteDTO.getId());
        return JsonResponse.success(b);
    }


    @PostMapping("/removeByIds")
    @ResponseBody
    public JsonResponse removeByIds(@RequestBody DeleteDTO deleteDTO){
        boolean b = whitelistSettingService.removeByIds(deleteDTO.getIds());
        return JsonResponse.success(b);
    }
    /*
     * 数据修改
     * 状态修改
     * 编辑
     * */

    @GetMapping("/getBySn")
    @ResponseBody
    public JsonResponse getBySn(String sn){
        System.out.println(sn);
        WhitelistSetting whitelistSetting=whitelistSettingService.getBySn(sn);
        return JsonResponse.success(whitelistSetting);
    }

    @PostMapping("/save")
    @ResponseBody
    public JsonResponse save(WhitelistSetting whitelistSetting){
        boolean b = whitelistSettingService.saveBySelf(whitelistSetting);
        System.out.println(b);
        return JsonResponse.success(b);
    }


    /*导出*/
    @GetMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response){
        whitelistSettingService.export(response);
    }

    /*导入模板*/
    @GetMapping("/exportTemp")
    public void exportTemp(HttpServletResponse response){
        whitelistSettingService.exportTemp(response);
    }

    /*文件上传导入*/
    @PostMapping("/inputUserByFile")
    @ResponseBody
    public JsonResponse inputUserByFile(MultipartFile file){
        Map<String,Object> msg=whitelistSettingService.inputUser(file);

        return JsonResponse.success(msg);
    }
    //FKU,让我PUSH！！！！！！！！！！！！！！！！！！！！！！
    @RequestMapping("/insert")
    @ResponseBody
    public JsonResponse insertUser(WhitelistSetting whitelistSetting){
        System.out.println(whitelistSetting.toString());
        boolean b = whitelistSettingService.save(whitelistSetting);
        return JsonResponse.success(b);
    }




}

